<?php

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
/**
 * Description of QuestionController
 *
 * @author hoangle
 */
class Backend_QuestionController extends Zend_Controller_Action {

    //put your code here
    public function init() {
        $this->_helper->layout->setLayout('backend');
        Service_Account::checkLogin(2);
    }
    
    public function index() {
        $this->_forward('get');
    }
    
    public function getAction() {
        $this->_helper->headTitle('Question Managment | Backend');

        $result = Service_Model::factory('Question', 'getQuestion', $arrParams, 'backend');

        $this->view->refuseanswer = $result;
    }

    /**
     * @desc List User Question
     * @return true
     */
    public function userquestionAction() {
        $arrQuestion = (array) Service_Model::factory('Question', 'getQuestion', NULL, 'backend');
        foreach ($arrQuestion as $val) {
            $arrRestQuest[$val['mackid']] = $val['mackname'];
        }

        $arrUserQuestion = Service_Model::factory('UserQuestion', 'getQuestionUser', NULL, 'backend');

        $this->view->question = $arrRestQuest;
        $this->view->userquestion = $arrUserQuestion;
    }

    /**
     * @desc Reply question of User
     */
    public function replyquestionAction() {
        $mackId = $_GET['mackid'];
        $mackName = $_GET['mackname'];

        if ($this->_request->isPost()) {
            $arrInput = $this->_request->getParams();
            $arrData['mackid'] = $arrInput['mackid'];
            $arrData['refuse_flag'] = ($arrInput['rd']==1)?2:(($arrInput['rd']==10)?10:0); //If cho refund is 2 else 0
            $arrData['answer'] = $arrInput['answer'];
            if($arrInput['rd'] == 10) {
                $arrData['answer'] = 'Không có đủ tư liệu để trả lời';
            }
            $result = Service_Model::factory('UserQuestion', 'replyAnswer', $arrData, 'backend');
            if($result) {
                //Auto refund credit to user
                $arrForRefund = Service_Model::factory('UserQuestion', 'getAutoRefund', $arrData, 'backend');
                if(!empty($arrForRefund)) {
                    foreach($arrForRefund as $value) {
                        //Auto refund credit
                        Service_Model::factory('UserQuestion', 'creaseCreditByUserId', $value['userid'], 'backend');
                        //Update refuse_flag
                        Service_Model::factory('UserQuestion', 'updateRefuseFlag', $value['id'], 'backend');
                    }
                }

                $this->_redirect('/backend/question/userquestion');
            }
        }

        $this->view->mackid = $mackId;
        $this->view->mackname = $mackName;
    }


}

?>
